/**
 * 
 */
package com.audi.box.server.handler;

import android.util.Log;

import com.audi.box.controller.IController;
import com.audi.box.controller.PlaceController;
import com.audi.box.server.message.HandlerException;
import com.audi.dto.PlaceItem;

/**
 * @author HuyPVA
 * 
 */
public class PlaceHandler extends Handler {
	private static PlaceHandler placeHandler;
	private static Object placeObj = new Object();

	/**
	 * @param controller
	 */
	private PlaceHandler(IController controller) {
		super(controller);
	}

	public static PlaceHandler getInstance(IController controller) {
		if (placeHandler == null) {
			synchronized (placeObj) {
				if (placeHandler == null) {
					placeHandler = new PlaceHandler(controller);
				}
				return placeHandler;
			}
		} else {
			return placeHandler;
		}
	}

	/*
	 * Get DeviceController
	 */
	public PlaceController getPlaceController() {
		return (PlaceController) getController();
	}

	/*
	 * Process message by action
	 */
	@Override
	public Object process(int actionId, String data) throws HandlerException {
		Object result = null;
		PlaceItem placeItem = null;

		switch (actionId) {
		case ADD_PLACE:
			placeItem = getGson().fromJson(data, PlaceItem.class);
			result = getPlaceController().addPlace(placeItem);
			break;
		case CHANGE_CAPABILITY:
			placeItem = getGson().fromJson(data, PlaceItem.class);
			result = getPlaceController().changeCapability(placeItem);
			break;
		case RENAME_PLACE:
			placeItem = getGson().fromJson(data, PlaceItem.class);
			result = getPlaceController().renamePlace(placeItem);
			break;
		case DELETE_PLACE:
			placeItem = getGson().fromJson(data, PlaceItem.class);
			result = getPlaceController().deletePlace(placeItem);
			break;
		case GET_ALL_PLACE:
			result = getPlaceController().getPlaceList();
			break;
		case GET_PLACE_BY_USER:
			int userId = getGson().fromJson(data, Integer.class);
			result = getPlaceController().getPlaceListByUser(userId);
			break;
		case CHECK_PLACE_EXIST:
			placeItem = getGson().fromJson(data, PlaceItem.class);
			result = getPlaceController().isExist(placeItem);
			break;
		case GET_PLACE_BY_ID:
			int placeId = getGson().fromJson(data, Integer.class);
			result = getPlaceController().getPlace(placeId);
			break;
		case UPDATE_PLACE:
			placeItem = getGson().fromJson(data, PlaceItem.class);
			result = getPlaceController().updatePlace(placeItem);
			break;
		case CHECK_PLACE_EXIST_BY_NAME:
			placeItem = getGson().fromJson(data, PlaceItem.class);
			result = getPlaceController().isExistByName(placeItem);
			break;
		default:
			Log.e("AudiBox", "[PlaceHandler.java] Function not support: " + actionId);
			throw new HandlerException("Function " + actionId +" not found!!!");
		}
		return result;

	}

}
